package com.shtel.bss.saas.tenant.controller;

import com.github.pagehelper.PageInfo;
import com.shtel.bss.saas.core.data.common.controller.DataController;
import com.shtel.bss.saas.tenant.dto.QrySysGlobalAuthResDTO;
import com.shtel.bss.saas.tenant.dto.SysGlobalAuthDTO;
import com.shtel.bss.saas.tenant.dto.SysTenantAuthJoinDTO;
import com.shtel.bss.saas.tenant.dto.SysTenantDTO;
import com.shtel.bss.saas.tenant.mapstrut.EntityDtoMapper;
import com.shtel.bss.saas.tenant.service.ISysGlobalAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 基础权限表 controller
 * @author xiezhiwei 2020-02-05 22:03
 */
@RestController
@RequestMapping("/bam/auths")
public class SysGlobalAuthController extends DataController{
    @Autowired
    ISysGlobalAuthService sysGlobalAuthService;

    @GetMapping()
    public PageInfo<QrySysGlobalAuthResDTO> querySysGlobalAuthList(@RequestParam(required = false) String authName,
                                                                   @RequestParam(required = false) String authCode,
                                                                   @RequestParam(required = false,defaultValue = "10") int pageSize,
                                                                   @RequestParam(required = false,defaultValue = "1") int pageNum){
        return EntityDtoMapper.INSTANCE.
                toQrySysGlobalAuthResDTOPageInfo(sysGlobalAuthService.querySysGlobalAuthList(authName,authCode,pageSize,pageNum));
    }

    @PostMapping()
    public void saveSysGlobalAuth(@RequestBody SysGlobalAuthDTO sysGlobalAuthDTO ){
        sysGlobalAuthService.saveSysGlobalAuth(EntityDtoMapper.INSTANCE.toSysGlobalAuthBo(sysGlobalAuthDTO));
    }

    @PutMapping()
    public void updateSysGlobalAuth(@RequestBody SysGlobalAuthDTO sysGlobalAuthDTO ){
        sysGlobalAuthService.updateSysGlobalAuth(EntityDtoMapper.INSTANCE.toSysGlobalAuthBo(sysGlobalAuthDTO));
    }

    @DeleteMapping("/{authId}")
    public void deleteSysTenant(@PathVariable("authId") Long[] authId){
        sysGlobalAuthService.removeSysGlobalAuth(Arrays.asList(authId));
    }

    /**
     * 授予租户
     * @param authId
     * @param sysTenantAuthJoinDTO
     */
    @PostMapping("tenants/{authId}")
    public void updateAuthTenant(@PathVariable("authId") Long authId,@RequestBody SysTenantAuthJoinDTO sysTenantAuthJoinDTO) {
        sysGlobalAuthService.updateAuthTenant(authId, sysTenantAuthJoinDTO.getTenantIds());
    }

    @GetMapping("showTree")
    public List<QrySysGlobalAuthResDTO> queryAuthTree(){
        return EntityDtoMapper.INSTANCE.toQrySysGlobalAuthResDTOList(sysGlobalAuthService.querySysGlobalAuthList());
    }

    @GetMapping("formTree")
    public List<QrySysGlobalAuthResDTO> queryFormAuthTree(){
        //类型(1 目录 2按钮)
        return EntityDtoMapper.INSTANCE.toQrySysGlobalAuthResDTOList(sysGlobalAuthService.querySysGlobalAuthList((byte)1));
    }

    /**
     * 查询已授予权限的租户
     * @param authId
     * @return
     */
    @GetMapping("tenants/grant/{authId}")
    public PageInfo<SysTenantDTO> querySysTenantGrantAuthId(@PathVariable("authId") Long authId,
                                                            @RequestParam(required = false,defaultValue = "10")int pageSize,
                                                            @RequestParam(required = false,defaultValue = "1") int pageNum){
        return EntityDtoMapper.INSTANCE.toSysTenantDTOPageInfo(sysGlobalAuthService.querySysTenantGrantAuthId(authId,pageSize,pageNum));
    }

    /**
     * 查询未授予权限的租户
     * @param authId
     * @return
     */
    @GetMapping("tenants/notGranted/{authId}")
    public PageInfo<SysTenantDTO> querySysTenantnotGrantedAuthId(@PathVariable("authId") Long authId,
                                                                 @RequestParam(required = false,defaultValue = "10")int pageSize,
                                                                 @RequestParam(required = false,defaultValue = "1")int pageNum){
        return EntityDtoMapper.INSTANCE.toSysTenantDTOPageInfo(sysGlobalAuthService.querySysTenantnotGrantedAuthId(authId,pageSize,pageNum));
    }

}